package com.liangtian.mdm.system.code.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.liangtian.mdm.base.controller.BaseController;
import com.liangtian.mdm.base.utils.ResultObject;
import com.liangtian.mdm.dm.common.page.JsonWrap;
import com.liangtian.mdm.util.RequestUtils;
import com.liangtian.mdm.system.code.model.Code;
import com.liangtian.mdm.system.code.service.CodeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/code")
public class CodeController extends BaseController {

    @Autowired
    CodeService codeServiceImp;

    // Session中保存的当前登录用户的信息
    //@Value("${ConstantBean.CURRENT_SESSION_USER}")
    //private String ConstantBean.CURRENT_SESSION_USER;


    @ResponseBody
    @RequestMapping(value = "/queryConfigCodes", method = RequestMethod.POST)
    public String queryConfigCodes(HttpServletRequest request) {
        Map<String, Object> resMap = new HashMap<>();
        Map<String, Object> param = RequestUtils.paramToMap(request);
        List<Code> codeLst = codeServiceImp.queryFieldCodes(param);
        List<String> codeTypeLst = new ArrayList<String>();
        for (Code code : codeLst) {
            codeTypeLst.add(code.getCodeType());
        }
        resMap.put("codeLst", codeLst);
        resMap.put("codeTypeLst", codeTypeLst);
        return JSON.toJSONString(resMap);
    }

    @SuppressWarnings("rawtypes")
    @ResponseBody
    @RequestMapping(value = "/queryCodeRuleList", method = RequestMethod.POST)
    public String queryCodeRuleList(HttpServletRequest request, Code code) {
        //Map<String, Object> param = RequestUtils.paramToMap(request);
        Map<String, Object> param = RequestUtils.paramToMap(request, Code.class, false);
        PageInfo pageInfo = codeServiceImp.queryCodeAttrs(param);
        List l = pageInfo.getList();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("rows", l);// JSONArray
        jsonObject.put("total", pageInfo.getTotal());
        return JsonWrap.toJSONString(jsonObject);
    }

    /*
     * @desc 新增编码配置信息
     */
    @RequestMapping(value = "/saveConfigCodes", method = RequestMethod.POST)
    @ResponseBody
    public ResultObject insertConfigCodes(HttpServletRequest request) {  //JSONObject
        //查询条件
        boolean ret = false;
        Map<String, Object> param = RequestUtils.paramToMap(request);
        try {
            List<Code> codeLst = codeServiceImp.addCodeList(param);
            List<Code> oldCodeLst = codeServiceImp.queryFieldCodes(param);
            if (oldCodeLst.isEmpty())
                ret = codeServiceImp.insertConfigCodes(codeLst);
            else {
                boolean delFlag = codeServiceImp.deleteConfigCodes(codeLst.get(0).getAttrId());
                if (delFlag)
                    ret = codeServiceImp.insertConfigCodes(codeLst);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        int f = ret ? 1 : 0;
        ResultObject result = new ResultObject(f, "", "");
        return result;
    }

    /*
     * @desc 删除编码配置信息
     */
    @RequestMapping(value = "/deleteCodesByAttrIdLst", method = RequestMethod.POST)
    @ResponseBody
    public ResultObject deleteCodesByAttrIdLst(HttpServletRequest request) {  //JSONObject
        //查询条件
        boolean ret = false;
        Map<String, Object> param = RequestUtils.paramToMap(request);
        try {
            ret = codeServiceImp.deleteConfigCodesByIds(param);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int f = ret ? 1 : 0;
        ResultObject result = new ResultObject(f, "", "");
        return result;
    }
}
